from django.db import models
from django.forms import ModelForm
from b9c.sign_generator.unique_slugify import unique_slugify
from django.conf import settings

import datetime

# Create your models here.

class Font(models.Model):
    font = models.CharField(max_length=100)

    def __unicode__(self):
        return self.font


class Sign(models.Model):
    slug = models.SlugField(help_text='Automatically built from the text.',max_length=50, blank=True)
    publish_date = models.DateTimeField(default=datetime.datetime.now, blank=True)
    sign_text = models.TextField(help_text='The text to display on the sign (Max 500 Characters). Note please use return - as the text is not word-wrapped on the 3d model.',max_length=500)
    x = models.DecimalField(help_text='Sign size X (e.g. 150)', max_digits=4, decimal_places=1)
    y = models.DecimalField(help_text='Sign size Y (e.g. 50). Give the special value of 0 and the sign will be sized around the text size exactly', max_digits=4, decimal_places=1)
    z = models.DecimalField(help_text='Sign size Z (e.g. 10)', max_digits=4, decimal_places=1)
    boarder = models.DecimalField(help_text='Minimum boarder, between text and edge of sign (e.g. 10)', max_digits=4, decimal_places=1)
    font = models.ForeignKey(Font)

    def __unicode__(self):
                return self.sign_text
    class Meta:
        ordering = ["-publish_date"]

    def get_absolute_url(self):
        return "/signs/%s/" % (self.slug)

    def get_png_url(self):
        return "/%s%s%s" % (settings.PNG_URL, self.slug, ".png")

    def get_png(self):
        return "/%s%s%s" % (settings.PNG_DIR, self.slug, ".png")
#
#    def get_zip(self, ext):
#        return "/%s%s%s" % (settings.ZIP_DIR, self.slug, ext)
#

    def save(self, **kwargs):
        slug_str = "%s %s" % (self.sign_text[:34], self.font.__unicode__()[:15]) 
        unique_slugify(self, slug_str) 
        super(Sign, self).save()

class SignForm(ModelForm):
    """
    Auto generated form to create Server models.
    """
    class Meta:
        model = Sign
        exclude = ('slug', 'publish_date')



